package com.webwork.lims.stock.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.stock.dataobject.Product;
import com.webwork.lims.stock.service.ProductService;
import com.webwork.lims.stock.service.StockService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/baseInfo")
public class ProductController extends SuperCtrl {

	@Autowired
	private ProductService productSve;
	
	@Autowired
	private StockService stockSve;
	
	@RequestMapping(value = "/products")
	@Override
	public String mainPage() {
		return "products";
	}
	
	@RequestMapping(value="loadSku")
	@ResponseBody
	public ResponseEntity<String> loadZone() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		List<Product> products = productSve.query(new Product());
		List<Object> ret = new ArrayList<Object>();
		for (Product o : products) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getSku());
			map.put("name", o.getSku() + " - " + o.getProductName());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "queryProductList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Product queryObj = new Product();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Product.class);
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", productSve.count(queryObj));
		ret.put("rows", productSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getProduct")
	@ResponseBody
	public ResponseEntity<String> getProduct(@RequestParam("sku") String sku) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Product product = productSve.get(sku);
		return new ResponseEntity<String>(new Gson().toJson(product).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addProduct")
	@ResponseBody
	public String addProduct(@RequestParam("jsonObj") String jsonObj, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Product product = new Gson().fromJson(jsonObj, Product.class);
		if (productSve.get(product.getSku()) != null) {
			return EXIST;
		}
		productSve.save(product);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editProduct")
	@ResponseBody
	public String editProduct(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Product p = new Gson().fromJson(jsonObj, Product.class);
		Product product = productSve.get(p.getSku());
		if (product == null) {
			return NOT_EXIST;
		}
		product.setProductName(p.getProductName());
		productSve.save(product);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delProduct")
	@ResponseBody
	public String delProduct(@RequestParam("sku") String sku, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Product product = productSve.get(sku);
		if (product == null) {
			return FAIL;
		}
		productSve.delete(sku);
		//同时删除对应库存
		stockSve.delStock(sku);
		
		return SUCCESS;
	}
	
}
